<template>
    <view>
        <active-btn
                v-if="activeNotice"
                @click="showActiveTip"
        />
        <active-tip
                v-if="activeTip"
                @confirm="startActive"
                @close="closeActiveTip"
                :activeResult="activeResult"
                :text="activeTipText"
                :confirmText="confirmText"
        />
        <money-rain v-if="activeRun" @end="activeEnd"></money-rain>
    </view>
</template>

<script>
    import MoneyRain from './money-rain.vue';
    import ActiveBtn from './active-btn.vue';
    import ActiveTip from './active-tip.vue';
    import dayjs from 'dayjs';

    const maxActiveCount = 30;
    const beginDate = '2019/06/11';
    const endDate = '2019/06/19';

    let timer;

    export default {
        components: {
            MoneyRain,
            ActiveBtn,
            ActiveTip,
        },

        async created() {
            if (this.activeDate) {
                if (uni.getStorageSync('token')) {
                    await this.getInActiveCount();
                    const count = this.activeCount;
                    if (count < maxActiveCount) {
                        document.body.style.position = 'fixed';
                        this.activeTip = true
                    }
                }
            }

        },
        destroyed() {
            timer && clearInterval(timer);
        },
        data() {
            return {
                activeRun: false,
                activeTip: false,
                activeResult: [],
                activeCount: maxActiveCount,
                openImg: '../../static/img/active-money-rain/open2.png',
            }
        },
        computed: {
            confirmText() {
                const leftCount = maxActiveCount - this.activeCount;
                if (leftCount === maxActiveCount) {
                    return `马上抢红包`
                } else if (leftCount > 0) {
                    return `再玩一次`
                } else {
                    return `收钱为安`
                }
            },
            activeTipText() {
                const leftCount = maxActiveCount - this.activeCount;
                if (leftCount > 0) {
                    return `今日您还可以再抢${leftCount}次`
                } else {
                    return `看来,一天抢${maxActiveCount}次还不过瘾...`
                }

            },

            activeNotice() {
                const flag = this.activeTip || this.activeRun;
                return !flag && this.activeDate && this.activeCount < maxActiveCount
            },
            activeDate() {
                return dayjs() > dayjs(beginDate) && dayjs() < dayjs(endDate)
            }
        },

        methods: {
            //关闭活动提示弹窗
            closeActiveTip() {
                document.body.style.position = 'static';
                this.activeTip = false;
                this.activeResult = [];
            },

            //抢红包结束
            activeEnd(count) {
                this.submitMoney(count);
            },
            //开始抢红包
            async startActive() {
                if (this.activeCount < maxActiveCount) {
                    // await this.getMoneyRain();
                    this.activeResult = [];
                    this.activeRun = true;
                    this.activeTip = false
                } else {
                    this.closeActiveTip()
                }
            },
            //打开抢红包提示
            async showActiveTip() {
                await this.getInActiveCount();
                document.body.style.position = 'fixed';
                this.activeTip = true;
            },
            //获取用户当日活动次数
            async getInActiveCount() {
                const data = await this.$ajax.get(this.$usereq + '/coupon-customer/customer/redenvelope/selectCount');
                data && this.updateCount(data.count);
            },
            updateCount(count) {
                this.activeCount = count;
            },
            //提交抢到的红包
            async submitMoney(count) {
                const data = await this.$ajax.upload(this.$usereq + '/coupon-customer/customer/redenvelope/save', '', {num: count});
                if (data) {
                    this.activeResult[0] = data.num;
                    this.activeResult[1] = count;
                    await this.showActiveTip();
                    this.activeRun = false;
                }
            },
        }
    };
</script>

